package com.nine.pesys.model.action;

import com.nine.pesys.base.BaseAction;
import com.nine.pesys.model.entity.Game;
import com.nine.pesys.util.annotation.Privilege;
import com.opensymphony.xwork2.ActionContext;
import net.sf.json.JSONArray;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.apache.struts2.json.annotations.JSON;

/**
 * Created by L先森 on 2014/12/23.
 */
@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage(value="my_json")
@Results({
        @Result(name = "index",location = "/front/home.jsp"),
        @Result(name = "equipments",location = "/dddddasda"),
        @Result(name = "bsIndex",type = "json",params = {"root","resultTree"},
        location = "/admin/house_edit.jsp"),
})
@InterceptorRefs({
        @InterceptorRef("myInterceptor"),
})
/**
 * 观看首页内容是不需要权限的，所以只有这个类不用权限拦截
    默认是有权限拦截的，但是只要改变了拦截器，默认拦截器就不会起作用了
 */
@Privilege
public class DataAction extends BaseAction<Game> {
    private JSONArray resultTree;

//    准备首页数据
    @Action("/index")
    public String index() throws Exception{
        //        准备赛事数据
        games = this.gameService.findAll();
        if(games.size()>0){
            ActionContext.getContext().put("games",games);
        }
//        准备系统公告数据
        announcements = this.announcementService.findAll();
        if (announcements.size()>0)
            ActionContext.getContext().put("announcements",announcements);
        return "index";
    }

    /**
     *准备器材页面数据
     * @return
     * @throws Exception
     */
    @Action("/equipmentIndex")
    public String equipmentIndex() throws Exception{
        this.equipments = this.equipmentService.findAll();
        if(equipments.size()>0){
            ActionContext.getContext().put("equipments",equipments);
        }
        return "equipments";
    }

    @Action("/bsIndex")
    public String bsIndex() throws Exception{
        jsonConfig.setExcludes(new String[]{"user","usPrivileges"});
        roles = this.roleService.findAll();
        resultTree = JSONArray.fromObject(roles,jsonConfig);
        return "bsIndex";
    }

    @JSON
    public JSONArray getResultTree() {
        return resultTree;
    }

    public void setResultTree(JSONArray resultTree) {
        this.resultTree = resultTree;
    }
}

